package com.example.xd.android_vresion_test.android_10;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.example.xd.android_vresion_test.R;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

import pub.devrel.easypermissions.EasyPermissions;

//compileSdkVersion 29
public class Android_10_Activity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_android_10_);
        findViewById(R.id.btn1).setOnClickListener(this);
        findViewById(R.id.btn2).setOnClickListener(this);
        findViewById(R.id.btn3).setOnClickListener(this);


    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn1:
                File android_10 = getExternalFilesDir("android_10");
                //  /storage/emulated/0/Android/data/com.example.xd.android_vresion_test/files/android_10
                Log.i("xd---", android_10.getAbsolutePath());
                break;
            case R.id.btn2:
                startActivity(new Intent(Android_10_Activity.this, ShowActivity.class));
                break;
            case R.id.btn3:
                downloadTest("", "");
                break;
        }
    }

    private void downloadTest(final String file, final String name) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
            Toast.makeText(this, "10之前", Toast.LENGTH_SHORT).show();
            return;
        }
        new Thread(new Runnable() {
            @RequiresApi(api = Build.VERSION_CODES.Q)
            @Override
            public void run() {
                try {
                    URL url = new URL(file);
                    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                    urlConnection.setRequestMethod("GET");
                    urlConnection.setConnectTimeout(8000);
                    urlConnection.setReadTimeout(8000);
                    InputStream inputStream = urlConnection.getInputStream();
                    BufferedInputStream bis = new BufferedInputStream(inputStream);
                    ContentValues contentValues = new ContentValues();
                    contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, name);
                    contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_DOWNLOADS);
                    Uri uri = getContentResolver().insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, contentValues);
                    if (uri != null) {
                        OutputStream outputStream = getContentResolver().openOutputStream(uri);
                        if (outputStream != null) {
                            BufferedOutputStream bos = new BufferedOutputStream(outputStream);
                            byte[] buffer = new byte[1024];
                            int bytes = bis.read(buffer);
                            while (bytes >= 0) {
                                bos.write(buffer, 0, bytes);
                                bos.flush();
                                bytes = bis.read(buffer);
                            }
                            bos.close();
                        }
                    }
                    bis.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }).start();
    }
}
